package com.book_manager.dao.LAndR;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



import com.book_manager.entity.LAndR;
import com.book_manager.util.BaseDao;



public class LAndRDaoImpl {
	/*
	 * ����˺ŷ���
	 */
	public boolean addUser(LAndR l){
		try{
			Connection con=BaseDao.getCon();
			PreparedStatement pstm=con.prepareStatement("insert into user(username,password,loginNum) values(?,?,?)");
			pstm.setString(1, l.getUsername());
			pstm.setString(2, l.getPassword());
			pstm.setInt(3, l.getLoginNum());
			int rs = pstm.executeUpdate();
			if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	/*
	 * ɾ���˺ŷ���
	 */
	public boolean deleteUserByid(int id){
		try{
			Connection con=BaseDao.getCon();
			PreparedStatement pstm=con.prepareStatement("delete from user where id=?");
			pstm.setInt(1, id);
			int rs=pstm.executeUpdate();
			if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	/*
	 * �������뷽��
	 */
	public boolean ResetUser(LAndR l){
		try{
			Connection con=BaseDao.getCon();
			PreparedStatement pstm=con.prepareStatement("update user set password=? where username=? and loginNum=?");
			pstm.setString(1, l.getPassword());
			pstm.setString(2,l.getUsername());
			pstm.setInt(3, l.getLoginNum());
            int rs = pstm.executeUpdate();
            System.out.println(rs);
            if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	/*
	 * �޸����뷽��
	 */
	public boolean updateUser(LAndR l){
		try{
			Connection con=BaseDao.getCon();
			PreparedStatement pstm=con.prepareStatement("update user set password=? where id=?");
			pstm.setString(1, l.getPassword());
			pstm.setInt(2, l.getId());
            int rs = pstm.executeUpdate();
            System.out.println(rs);
            if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	/*
	 * ����id��ѯ��Ӧ�˺�
	 */
	public LAndR findUserById(int id){
		try{
			Connection con=BaseDao.getCon();
			PreparedStatement pstm=con.prepareStatement("select * from user where id=?");
			pstm.setInt(1, id);
			ResultSet rs=pstm.executeQuery();
			LAndR l=null;
			while(rs.next()){
				l=new LAndR();
				l.setId(rs.getInt("id"));
				l.setUsername(rs.getString("username"));
				l.setName(rs.getString("name"));
				l.setSex(rs.getString("sex"));
				l.setPhone(rs.getString("phone"));
				l.setAddress(rs.getString("address"));
				l.setPower(rs.getString("power"));
				l.setPassword(rs.getString("password"));
				l.setLoginNum(rs.getInt("loginNum"));
				
			}
			return l;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/*
	 * ����username��ѯ�˺�
	 */
	public LAndR findUserByUserName(String username){
		try{
			Connection con=BaseDao.getCon();
			PreparedStatement pstm=con.prepareStatement("select * from user where username=?");
			pstm.setString(1, username);
			ResultSet rs=pstm.executeQuery();
			LAndR l=null;
			while(rs.next()){
				l=new LAndR();
				l.setId(rs.getInt("id"));
				l.setUsername(rs.getString("username"));
				l.setName(rs.getString("name"));
				l.setSex(rs.getString("sex"));
				l.setPhone(rs.getString("phone"));
				l.setAddress(rs.getString("address"));
				l.setPower(rs.getString("power"));
				l.setPassword(rs.getString("password"));
				l.setLoginNum(rs.getInt("loginNum"));
			}
			return l;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*
	 * ��¼�˺ŵķ���
	 */
	public boolean loginUser(LAndR l){
		try{
			Connection con=BaseDao.getCon();
			PreparedStatement pstm=con.prepareStatement("select * from user where username=? and password=?");
			pstm.setString(1, l.getUsername());
			pstm.setString(2, l.getPassword());
			ResultSet rs=pstm.executeQuery();
			l=new LAndR();
			 if(rs.next()) {
				 rs.previous();
				 while(rs.next()){
					 l.setId(rs.getInt("id"));
						l.setUsername(rs.getString("username"));
						l.setName(rs.getString("name"));
						l.setSex(rs.getString("sex"));
						l.setPhone(rs.getString("phone"));
						l.setAddress(rs.getString("address"));
						l.setPower(rs.getString("power"));
						l.setPassword(rs.getString("password"));
						l.setLoginNum(rs.getInt("loginNum"));
					 System.out.println(l);
				 }
				 return true;	 
			 }else {
				 return false;
				 
			 }
		} catch (SQLException e) {
			
			e.printStackTrace();
			return false;
		}
	}
	/*
	 * ��֤���˺��Ƿ����<����id>
	 */
	public boolean isExist(int id){
		return findUserById(id)==null?false:true;
	}
	/*
	 * ��֤���˺��Ƿ����<����username>
	 */
	public boolean isExist(String username){
		return findUserByUserName(username)==null?false:true;
	}
	
}
